package com.bicon.botu.finance.payment.unionpay;

import com.bicon.botu.finance.payment.entity.UnionpayDto;
import com.bicon.botu.finance.payment.unionpay.sdk.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

/**
 * @Author: 宋旭
 * @Date: Created in 16:01 2017/7/31/031
 */
@RestController
public class UnionPayApi {

    private static SDKConfigController sdkConfig;

    private static CertUtil certUtil;

    @Autowired
    public void setCertUtil(CertUtil certUtil) {
        UnionPayApi.certUtil = certUtil;
    }

    @Autowired
    public void setSdkConfig(SDKConfigController sdkConfig) {
        UnionPayApi.sdkConfig = sdkConfig;
    }

    /**
     * 银联全渠道平台发送订单推送请求
     *
     * @param unionpayDto
     * @param orderNum
     * @param total
     * @param reqReserved
     * @param backUrl
     * @param txnTime
     * @return
     */
    public static Map<String, String> payToUnionpay(UnionpayDto unionpayDto, String orderNum, String total, String reqReserved, String backUrl, Date txnTime) {
        setParameter(unionpayDto);
        certUtil.init();
        String txnAmt = String.valueOf(Integer.parseInt(total) * 100);
        Map<String, String> reqData = UnionPayConfig.createParamUnionpay(unionpayDto.getUnionMerId(), orderNum, txnAmt, reqReserved, backUrl, txnTime, sdkConfig);
        Map<String, String> rspData = AcpService.post(reqData, sdkConfig.getAcpsdkAppTransUrl(), sdkConfig.getAcpsdkEncoding());
        return rspData;
    }

    private static void setParameter(UnionpayDto unionpayDto) {
        sdkConfig.setAcpsdkSignCertPath(SDKUtil.getFilePath(unionpayDto.getSignCertPath()));
        sdkConfig.setAcpsdkSignCertPwd(unionpayDto.getSignCertPwd());
        sdkConfig.setAcpsdkEncryptCertPath(SDKUtil.getFilePath(unionpayDto.getEncryptCert()));
        sdkConfig.setAcpsdkMiddleCertPath(SDKUtil.getFilePath(unionpayDto.getMiddleCert()));
        sdkConfig.setAcpsdkRootCertPath(SDKUtil.getFilePath(unionpayDto.getRootCert()));
    }

    /**
     * 定单查询
     *
     * @return
     */
    public static Map<String, String> queryOrder(String merId, String orderNum, Date txnTime) {
        Map<String, String> reqData = UnionPayConfig.createParamQuery(merId, orderNum, txnTime);
        String requestAppUrl = SDKConfig.getConfig().getSingleQueryUrl();
        Map<String, String> rspData = AcpService.post(reqData, requestAppUrl, DemoBase.encoding);
        return rspData;
    }

    /**
     * 银联全渠道平台发送代付推送请求
     */
    public static Map<String, String> daifuToUnionpay(String merId, String orderNum, float total, String reqReserved, String backUrl,
                                                      Date txnTime, String remark, String identityCard, String payeeName, String payeeAccount) {
        String txnAmt = (total * 100) + "";
        Map<String, String> reqData = UnionPayConfig.createParamDaifu(merId, orderNum, txnAmt, reqReserved, backUrl, txnTime, remark, identityCard, payeeName, payeeAccount);
        String requestAppUrl = SDKConfig.getConfig().getBackRequestUrl();
        Map<String, String> rspData = AcpService.post(reqData, requestAppUrl, DemoBase.encoding);
        return rspData;
    }

}
